<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="login-title">管理员登录</div>
      <el-form ref="loginForm" :model="loginForm" :rules="rules" label-width="100px" class="login-form">
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="loginForm.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="loginForm.password" placeholder="请输入密码" @keyup.enter="submitForm"></el-input>
        </el-form-item>
        <el-form-item class="button-group">
			<el-button type="primary" @click="goToUpdate" class="reset-password-button">修改密码</el-button>
          <el-button type="primary" @click="submitForm">登录</el-button>
          
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        email: '',
        password: ''
      },
      rules: {
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          { validator: this.validateEmail, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { validator: this.validatePassword, trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    validateEmail(rule, value, callback) {
      if (value === '') {
        callback(new Error('邮箱不能为空'));
      } else if (!/\S+@\S+\.\S+/.test(value)) {
        callback(new Error('请输入正确的邮箱地址'));
      } else {
        callback();
      }
    },
    validatePassword(rule, value, callback) {
      if (value.length < 6) {
        callback(new Error('密码长度必须大于6位'));
      } else {
        callback();
      }
    },
    submitForm() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          const params = new URLSearchParams();
          params.append('email', this.loginForm.email);
          params.append('password', this.loginForm.password);
          
          axios({
            method: 'post',
            url: 'http://localhost:8887/admin/login',
            data: params,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
          }).then(response => {
            if (response.data.code === 200) {
              localStorage.setItem('Authorization', response.data.data);
              alert('登录成功!');
              this.$router.push('/');
            } else {
              alert(response.data.message);
            }
          }).catch(error => {
            console.error('登录请求失败:', error);
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    goToUpdate() {
      this.$router.push('/update');
    }
  }
};
</script>

<style scoped>
.login-container {
  background-image: url('@/assets/background.png');
  background-size: cover;
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
.login-card {
  width: 400px;
  /* padding: 20px; */
  /* display: flex; */
  background-color: ;
  flex-direction: column;
  align-items: center;
}
.login-title {
  text-align: center;
  font-size: 18px;
  margin: 20px 0;
}
.login-form {
	margin-right:40px;
  display: flex;
  flex-direction: column;
}
.button-group {
	margin-right:40px;
  display: flex;
  justify-content: space-between;
}
.reset-password-button {
  background-color: #fff;
  color: #409eff;
  border-color: #409eff;
}
.reset-password-button:hover {
  background-color: #409eff;
  color: #fff;
}
</style>